package leeon.kaixin.wap.models;

import java.io.Serializable;

import leeon.mobile.BBSBrowser.utils.HTMLUtil;

public class Repaste implements Serializable {
	
	private static final long serialVersionUID = -5379278379168483957L;
	
	public static Repaste newInstance(String htmlDiv) {
		if (htmlDiv == null || htmlDiv.length() == 0) return null;
		//<img src="http://wapimg.kaixin001.com.cn/wapi2/repaste/sy_dt_rj1.gif" height="16" width="16"/>
		//<a href="/repaste/detail.php?uid=53123160&amp;urpid=4471818085&amp;verify=53123160_2538938_1300858831_5c0f3518f33ea392418850210e1685d3_kx&amp;
		//url=%2Frepaste%2Findex.php%3Fuid%3D53123160%26verify%3D2538938_2538938_1300858633_8b39ccc1f3e0d670ea135d99f1dba16c_kx%26url%3D%252Fhome%252F%253Fuid%253D2538938%2526verify%253D2538938_2538938_1300858523_42e88b1fa93cf4a0b3043b452b735cd9_kx">
		//姑娘，我就是喜欢你的奔放~</a><br/>
		//<span class="c9">2011-03-19 20:47</span>
		
		//<div class="recm" onclick="" tohref="/repaste/detail.php?uid=64057526&amp;urpid=6067844918&amp;verify=64057526_2538938_1325310440_1f70e2630d100aea6682e77503e76772_kx&amp;url=%2Frepaste%2Findex.php%3Fuid%3D64057526%26verify%3D2538938_2538938_1325310322_823c48ead9e7cfb21936beb505abae49_kx%26url%3D%252Fhome%252F%253Fnewstype%253D1088%2526verify%253D2538938_2538938_1325310319_8cd1896d14b993cec65c9e503aadc33b_kx%2526tip%253D1">
		//<p class="f16"><img style='margin-top:1px' align='top' src="http://wapimg.kaixin001.com.cn/wapi2/repaste/sy_dt_rj1.gif" height="16" width="16"/>&nbsp;<a>网友推选的“中国恶人”出来了</a></p>
		//<p class="f14">经网友提名推选，2011年度恶人为：　　临时工　　农二代　　为人民服务...</p>
		//<p class="f12">
		//<span class="mt5">
		//<i class="rpst_time" style="color:#999">12-31 13:42</i>
		//<i class="r">
		//<i class="btn_rpst" style="color:#336699">2088</i>
		//<i class="btn_stow" style="color:#336699">1107</i>
		//</i>
		//</span>
		//<span class="r"><a class="quickrp" data-uid="64057526" data-urpid="6067844918" data-rtype="repaste_64057526_50231117-6067844918-3"><img src="http://wapimg.kaixin001.com.cn/wapi2/repaste/btn_rpst.png"/></a></span>
		//</p>
		//</div>
		Repaste r = new Repaste();
		r.uid = HTMLUtil.findStr(htmlDiv, "tohref=\"/repaste/detail.php?uid=", "&amp");
		r.urpid = HTMLUtil.findStr(htmlDiv, "urpid=", "&amp");
		r.date = HTMLUtil.findStr(htmlDiv, "<i class=\"rpst_time\" style=\"color:#999\">", "</i>");
		
		r.title = HTMLUtil.findStrRegex(htmlDiv, "<a[^>]*>", "</a[^>]*>");
		if (htmlDiv.indexOf("sy_dt_rj1.gif") != -1) {
			r.type = RepasteType.日记;
		} else if (htmlDiv.indexOf("link1.gif") != -1) {
			r.type = RepasteType.网页;
		} else {
			r.type = RepasteType.其他;
		}
		return r;
	}
	
	public static boolean repasteContent1(String htmlDiv, Repaste r) {
		String[] h = HTMLUtil.findStrs(htmlDiv, "<div class=\"list pd8\">", "</div><div");
		int t;
		if (h.length >= 2) {
			if (h[0] != null && h[0].length() != 0) {
				r.title = HTMLUtil.findStr(h[0], "<b>", "</b>");
			}
			
			if (h[1] == null || h[1].length() == 0) return false;
			//more than one page
			if ((t = h[1].indexOf("<div class=\"list1\">")) != -1) {
				String s = h[1].substring(0, t).trim(); 
				if (s.length() == 0)
					return false;
				else { 
					r.content += s;
					return true;
				}
			//one page
			} else if ((t = h[1].lastIndexOf("<br/>")) != -1) {
				r.content += h[1].substring(0, t).trim();
				return false;
			}
		}
		return false;
	}
	
	public static boolean repasteContent(String html, Repaste r) {
		//查找标题
		String titleDiv = HTMLUtil.dealEnclosingTag(html, "div", "class=\"rpst_tit\"", false);
		r.title = HTMLUtil.dealHref(titleDiv)[1];
		
		//查找内容
		String contentDiv = HTMLUtil.dealEnclosingTag(html, "div", "class=\"rpst_cont\"", false);
		//去掉转载人
		int e = contentDiv.indexOf("<p class=\"mt5 fix\">");
		if (e != -1) contentDiv = contentDiv.substring(0, e);
		
		//查找分页
		String pageDiv = HTMLUtil.dealEnclosingTag(html, "div", "id=\"iscrollpager\"", false);
		if (pageDiv == null) {
			r.content = contentDiv;
			return false;
		} else {
			r.content += contentDiv;
			//查找分页的信息
			//<input type="hidden" id="page" name="page" value="0" />
			//<input type="hidden" id="totalpages" name="totalpages" value="2" />
			String p = HTMLUtil.findStr(pageDiv, "name=\"page\" value=\"", "\"");
			String t = HTMLUtil.findStr(pageDiv, "name=\"totalpages\" value=\"", "\"");
			if (Integer.parseInt(p)+1 < Integer.parseInt(t)) {
				return true;
			}
		}
		return false;
	}

	private String uid;
	
	private String urpid;
	
	private String title;
	
	private String content = "";
	
	private String date;
	
	private RepasteType type;
	
	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getUrpid() {
		return urpid;
	}

	public void setUrpid(String urpid) {
		this.urpid = urpid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	public RepasteType getType() {
		return type;
	}

	public void setType(RepasteType type) {
		this.type = type;
	}
	
}
